import 'package:flutter/material.dart';
import 'Toast.dart';
import 'package:dio/dio.dart';

class LoginWidget extends StatefulWidget {
  @override
  _LoginWidgetState createState() => _LoginWidgetState();
}


class _LoginWidgetState extends State<LoginWidget> {
  TextEditingController nameController = new TextEditingController();
  TextEditingController pwController = new TextEditingController();
  final greenColor = Color(0xff11B57C);

  bool isShowClose = false;

  _clearPw() {
    pwController.text = "";
    isShowClose = false;
    setState(() {});
  }

  _showClose() {
    isShowClose = pwController.text.length > 0;
    setState(() {});
  }

  _forgetPw() {
    //忘记密码
    Toast.toast(context, "忘记密码");
  }

  _login() {
//    if (nameController.text.length == 0) {
//      Toast.toast(context, "用户名不能为空");
//      return;
//    }
//    if (pwController.text.length == 0) {
//      Toast.toast(context, "密码不能为空");
//      return;
//    }
    getFuli();
  }

  _register() {
    Toast.toast(context, "注册");
  }

  _qqLogin() {
    Toast.toast(context, "QQ登录");
  }

  _weiboLogin() {
    Toast.toast(context, "微博登录");
  }

  void getFuli() async {
    try {
      Dio dio = new Dio();
      Options options = new Options(
          baseUrl:
              "https://mapi.yiguo.com/MobileApi.aspx?token=CA0C4043E9594934BB51BCE0AD9D36EB&v=4.3.8&os=android&sign=3bcbb706e5c50a2d6a53c05b0403ee38",
          connectTimeout: 5000,
          receiveTimeout: 5000);
      dio.options = options;
      Future future = dio.post("&apicode=yiguo.mapi.user.login", data: {
        "UserName": "4s\/CtcQVSgRT+7HBOK+Img==",
        "Password": "T83PAZsPHY\/3j1NvvMg+iA=="
//        "UserName": nameController.text,
//        "Password": pwController.text
      });
      future.then((data) {
        print(data);
      }).catchError((error) {
        print(error);
      });
    } catch (e) {
      return print(e);
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.grey[100],
      appBar: PreferredSize(
          child: AppBar(
            leading: Builder(builder: (context) {
              return IconButton(
                icon: Icon(
                  Icons.arrow_back,
                  color: Colors.black54,
                ),
                onPressed: () {
                  Navigator.pop(context);
                },
              );
            }),
            backgroundColor: Colors.white,
            elevation: 0.0,
            centerTitle: true,
            title: Text(
              "登录",
              style: TextStyle(color: Colors.black),
            ),
          ),
          preferredSize: Size.fromHeight(45)),
      body: Container(
        padding: EdgeInsets.only(top: 10),
        child: Wrap(
          crossAxisAlignment: WrapCrossAlignment.start,
          children: <Widget>[
            Container(
              color: Colors.white,
              padding: EdgeInsets.only(left: 10, right: 10),
              child: TextField(
                autofocus: false,
                controller: nameController,
                decoration: InputDecoration(
                    icon: Image.asset(
                      "images/im_longin_usericon.png",
                      width: 20,
                      height: 20,
                    ),
                    border: InputBorder.none,
//                    labelText: "用户名",
                    hintText: "用户名/手机/邮箱"),
              ),
            ),
            Container(
              color: Colors.white,
              height: 1,
              padding: EdgeInsets.only(left: 10, right: 10),
              child: Divider(
                height: 1,
//                indent: 10,
              ),
            ),
            Container(
              color: Colors.white,
              padding: EdgeInsets.only(left: 10),
              child: TextField(
                onChanged: (v) {
                  _showClose();
                },
                autofocus: false,
                controller: pwController,
                decoration: InputDecoration(
                    suffixIcon: isShowClose
                        ? GestureDetector(
                            onTap: () {
                              _clearPw();
                            },
                            child: Icon(
                              Icons.close,
                              size: 20,
                              color: Colors.grey,
                            ))
                        : null,
                    icon: Image.asset(
                      "images/im_longin_pwdicon.png",
                      width: 20,
                      height: 20,
                    ),
                    border: InputBorder.none,
                    hintText: "密码"),
                obscureText: true,
              ),
            ),
            Divider(
              height: 1,
            ),
            Padding(
              padding: EdgeInsets.only(left: 15, top: 10, bottom: 30),
              child: GestureDetector(
                child: Text(
                  "忘记密码？",
                  style: TextStyle(color: greenColor, fontSize: 14),
                ),
                onTap: () {
                  _forgetPw();
                },
              ),
            ),
            Row(
//              crossAxisAlignment:CrossAxisAlignment.center ,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                Container(
                  height: 28,
                  width: 160,
                  child: RaisedButton(
                    child: Text("登录"),
                    elevation: 0,
                    color: greenColor,
                    textColor: Colors.white,
                    onPressed: () {
                      _login();
                    },
                  ),
                ),
                Container(
                  height: 28,
                  width: 160,
                  child: OutlineButton(
                    child: Text("立即注册"),
                    color: Colors.white,
                    borderSide: BorderSide(color: greenColor),
                    textColor: greenColor,
                    onPressed: () {
                      _register();
                    },
                  ),
                )
              ],
            ),
            Padding(
                padding: EdgeInsets.only(left: 15, right: 15, top: 40),
                child: Image.asset("images/login_line.png")),
            Padding(
              padding: EdgeInsets.only(top: 40, left: 40, right: 40),
              child: Flex(
                direction: Axis.horizontal,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        _qqLogin();
                      },
                      child: Image.asset(
                        "images/login_qq_icon.png",
                        width: 60,
                        height: 60,
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        _weiboLogin();
                      },
                      child: Image.asset(
                        "images/login_weibo_icon.png",
                        width: 60,
                        height: 60,
                      ),
                    ),
                  )
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: 60),
              child: Center(
                child: Image.asset(
                  "images/yiguo_logo.png",
                  height: 50,
                  fit: BoxFit.fitHeight,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
